package com.arcvideo.acsplayer.httprequest;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.entity.StringEntity;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

import android.net.Uri;
import android.text.TextUtils;
import android.util.Log;

import com.arcvideo.acsplayer.utils.JsonUtil;

/**
 * BaseHttpRequest
 *
 * @author skl5250 所有http 网络请求的基类，http 请求必须继承这个类
 */

public abstract class BaseHttpRequest extends ParallelAsyncTask<Object, Object, Object> {

	private Uri.Builder mBuilder = null;
	private HttpClient mHttpClient = null;
	protected BaseHttpRequest mRequest = null;
	private List<NameValuePair> mPostParamList;
	protected HttpRequestCallback mCallback = null;
	protected HttpResponeCode mResponeCode = HttpResponeCode.HTTPRESPONE_OK;

	public interface HttpRequestCallback {
		public void OnRequestReturn(BaseHttpRequest request, Object data);
	};

	public static void initBaseHttpRequest() {
		// TODO 这个方法不能注释，必须要在Application初始化时调用，保证AsyncTask在主线程被初始化。
	}

	public void setHttpRequestCallback(HttpRequestCallback callback) {
		mCallback = callback;
	}

	public BaseHttpRequest(HttpClient httpClient) {
		mBuilder = new Uri.Builder();
		mHttpClient = httpClient;
	}

	@Override
	protected void onPreExecute() {

	}

	public HttpResponeCode getResponeCode() {
		return mResponeCode;
	}

	protected void onPostExecute(final Object v) {
		if (mCallback != null) {
			mCallback.OnRequestReturn(this, v);
		}
	}

	protected void appendGetParameter(String key, String value) {
		if (TextUtils.isEmpty(key)) {
			return;
		}
		mBuilder.appendQueryParameter(key, value);
	}

	protected void appendPostParameter(String key, String value) {
		if (TextUtils.isEmpty(key)) {
			return;
		}
		if (mPostParamList == null) {
			mPostParamList = new ArrayList<NameValuePair>();
		}
		BasicNameValuePair param = new BasicNameValuePair(key, value);
		mPostParamList.add(param);
	}

	protected void appendGetParameter(Map<String, String> parameters) {
		if (parameters != null) {
			for (Map.Entry<String, String> entry : parameters.entrySet()) {
				mBuilder.appendQueryParameter(entry.getKey(), entry.getValue());
			}
		}
	}

	protected void buildUrl(String scheme, String authority, String path) {
		mBuilder.scheme(scheme);
		mBuilder.authority(authority);
		mBuilder.path(path);
	}

	protected void buildUrl(String url) {
		mBuilder = Uri.parse(url).buildUpon();
	}

	protected void buildFromUrl(String url) {
		mBuilder = Uri.parse(url).buildUpon();
	}

	private String getUrl() {
		return mBuilder.build().toString();
	}

	public String executeJsonGetResult() {
		String url = getUrl();
		if ((mHttpClient != null) && (url != null)) {
			Log.e("http request url", "http request url ===== " + url);

			HttpResponse response = null;
			String body = null;
			HttpGet httpGet = new HttpGet(url);
			try {
				response = mHttpClient.execute(httpGet);
				body = JsonUtil.getJsonStringFromHttpResponse(response);
				Log.e("http request url", "http request body ===== " + body);
			} catch (Exception e) {
				e.printStackTrace();
				if (response == null) {
					if (httpGet != null) {

						if (TextUtils.isEmpty(body)) {
							mResponeCode = HttpResponeCode.HTTPRESPONE_TIMEOUT;
						}
					}
				} else {
					mResponeCode = HttpResponeCode.HTTPRESPONE_UNKNOWN;
				}
			}
			return body;
		}
		return null;
	}

	public String executeGetResult() {
		String url = getUrl();
		if ((mHttpClient != null) && (url != null)) {
			HttpResponse response = null;
			String body = null;
			HttpGet httpGet = new HttpGet(url);
			try {
				response = mHttpClient.execute(httpGet);
				body = EntityUtils.toString(response.getEntity(), HTTP.UTF_8);

			} catch (Exception e) {
				e.printStackTrace();
				if (response == null) {
					if (httpGet != null) {

						if (TextUtils.isEmpty(body)) {
							mResponeCode = HttpResponeCode.HTTPRESPONE_TIMEOUT;
						}
					}
				} else {
					mResponeCode = HttpResponeCode.HTTPRESPONE_UNKNOWN;
				}
			}
			return body;
		}
		return null;
	}

	public String executePostResult(byte[] data) {
		String url = getUrl();

		if ((mHttpClient != null) && (url != null)) {
			HttpResponse response = null;
			String body = null;
			StringEntity sEntity = null;
			HttpPost httpPost = new HttpPost(url);
			try {
				if (data != null) {
					sEntity = new StringEntity(new String(data).replace("=", ":"), HTTP.UTF_8);
				} else {
					sEntity = new UrlEncodedFormEntity(mPostParamList, HTTP.UTF_8);
				}
				sEntity.setContentType("application/json");
				httpPost.setEntity(sEntity);

				response = mHttpClient.execute(httpPost);
				body = JsonUtil.getJsonStringFromHttpResponse(response);

				return body;
			} catch (Exception e) {
				e.printStackTrace();
				if (response == null) {
					if (httpPost != null) {

						if (TextUtils.isEmpty(body)) {
							mResponeCode = HttpResponeCode.HTTPRESPONE_TIMEOUT;
						}
					}
				} else {
					mResponeCode = HttpResponeCode.HTTPRESPONE_UNKNOWN;
				}
				return body;
			}
		}
		return null;
	}

	public String executeJsonPostResult(byte[] data) {
		String url = getUrl();
		if ((mHttpClient != null) && (url != null)) {
			Log.e("http request url", "http request url ===== " + url);
			HttpResponse response = null;
			String body = null;
			HttpPost httpPost = new HttpPost(url);
			try {
				if (data != null) {
					httpPost.setEntity(new ByteArrayEntity(data));
				} else {
					httpPost.setEntity(new UrlEncodedFormEntity(mPostParamList, HTTP.UTF_8));
				}
				response = mHttpClient.execute(httpPost);
				body = JsonUtil.getJsonStringFromHttpResponse(response);

				return body;
			} catch (Exception e) {
				e.printStackTrace();
				if (response == null) {
					if (httpPost != null) {

						if (TextUtils.isEmpty(body)) {
							mResponeCode = HttpResponeCode.HTTPRESPONE_TIMEOUT;
						}
					}
				} else {
					mResponeCode = HttpResponeCode.HTTPRESPONE_UNKNOWN;
				}
				return body;
			}
		}
		return null;
	}

}